package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Nh;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class NhForm extends BasePage implements Serializable {
    private GenericManager<Nh, Long> nhManager;
    private Nh nh = new Nh();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    
    public void setNhManager(GenericManager<Nh, Long> nhManager) {
        this.nhManager = nhManager;
    }

    public Nh getNh() {
        return nh;
    }

    public void setNh(Nh nh) {
        this.nh = nh;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        nhManager.remove(nh.getId());
        addMessage("nh.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            nh = nhManager.get(id);
        } else {
            nh = new Nh();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (nh.getId() == null || nh.getId() == 0);
        // set the related jbqk as a foreign key.
        nh.setJbqk( jbqk );
        nhManager.save(nh);

        String key = (isNew) ? "nh.added" : "nh.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 